package no.ntnu.fp.controllers;

import java.sql.ResultSet;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import acm.program.ConsoleProgram;

import no.ntnu.fp.console.Options;
import no.ntnu.fp.storage.DBConnection;

public class MessageController extends AbstractController {
	private ConsoleProgram caller;
	
	public MessageController(ConsoleProgram caller) {
		this.caller = caller;
	}

	public boolean hasMoteinnkallinger(String username) {
		DBConnection db = new DBConnection();
		
		String query = "select InnkallingsID " +
				"from Moteinnkalling " +
				"where Status = " +  "\"" + "Avventer" + "\"" + " " +
						"and Deltaker = " + "\"" + username + "\"";
		ResultSet rs = db.makeSingleQuery(query);
		setResultSet(rs);
		if (next()) {
			closeDBConnection(db);
			return true;
		}
		return false;
		
	}

	public void showOfflineMessages(String username) {
		DBConnection db = new DBConnection();

		String query = "select Avtale.AvtaleID, Sted, Dato, Starttidspunkt, Sluttidspunkt, Beskrivelse " +
		"from Avtale, Moteinnkalling " +
		"where Avtale.AvtaleID = Moteinnkalling.AvtaleID" +
		" and Status = " +  "\"" + "Avventer" + "\"" + " " +
				"and Deltaker = " + "\"" + username + "\"";
		ResultSet rs = db.makeSingleQuery(query);
		setResultSet(rs);
		
		String start = "";
		String slutt = "";
		String beskrivelse = "";
		String dato = "";
		int aID = -1;
		ArrayList<String> avtaleStrenger = new ArrayList<String>();
		ArrayList<Integer> aIDs = new ArrayList<Integer>();
		
		while (next()) {
			start = getString("Starttidspunkt");
			slutt = getString("Sluttidspunkt");
			beskrivelse = getString("Beskrivelse");
			dato = getString("Dato");
			aID = getInt("AvtaleID");
			String avtaleStreng = dato + ",   " + "Start: " + start + ",   " + "Slutt: " + slutt + ",   " + beskrivelse;
			avtaleStrenger.add(avtaleStreng);
			aIDs.add(aID);
		}
		closeDBConnection(db);
		String[] avtaleStrengArray = new String[avtaleStrenger.size()];
		avtaleStrenger.toArray(avtaleStrengArray);
		
		int input = showChoices(avtaleStrengArray, "Velg avtale for a gi tilbakemelding", "Avventende avtaler");
		respondToInvite(aIDs.get(input), username);
	}
	
	private void respondToInvite(Integer aID, String userName) {
		String[] choices = {"Godta","Avsla"};
		int input = showChoices(choices, "Godta eller avsla moteinnkalling?", "Tilbakemelding moteinnkalling");
		
		switch (input) {
		case(0): System.out.println("Godtar moteinnkalling"); acceptInvite(aID, userName); break;
		case(1): refuseInvite(aID, userName); break;
		}
	}

	private void refuseInvite(Integer aID, String userName) {
		DBConnection db = new DBConnection();
		String query = "update Moteinnkalling " +
				"set Status = " + "\"" + "Avslatt" + "\"" + " " +
				"where AvtaleID = " + aID + " " +
				"and Deltaker = " + "\"" + userName + "\"";
		db.makeUpdate(query);
		closeDBConnection(db);
	}
	
	public boolean hasMessageToMeetingLeader(String userName) {
		DBConnection db = new DBConnection();
		String query = "select Moteinnkalling.AvtaleID " +
				"from Moteinnkalling, OppretterMote " +
				"where Moteinnkalling.AvtaleID = OppretterMote.AvtaleID " +
				"and Moteleder = " + "\"" + userName + "\"";
		ResultSet rs = db.makeSingleQuery(query);
		setResultSet(rs);
		if (next()) {
			closeDBConnection(db);
			return true;
		} else {
			closeDBConnection(db);
			return false;
		}
	}

	private void acceptInvite(Integer aID, String userName) {
		DBConnection db = new DBConnection();
		String query = "update Moteinnkalling " +
				"set Status = " + "\"" + "Godtatt" + "\"" + " " +
				"where AvtaleID = " + aID + " " +
				"and Deltaker = " + "\"" + userName + "\"";
		db.makeUpdate(query);
		closeDBConnection(db);
	}

	private int showChoices(String[] choices, String message, String nameOnMessageBox) {
		int input = JOptionPane.showOptionDialog(
				null                    
				, message    
				, nameOnMessageBox            
				, JOptionPane.YES_NO_OPTION 
				, JOptionPane.PLAIN_MESSAGE
				, null                    
				, choices                   
				, "Avtale"  
				);
		return input;
	}
	
	public void retrieveMessage(String username) {
		DBConnection db = new DBConnection();
		String query = "select Moteinnkalling.AvtaleID, Deltaker " +
				"from Moteinnkalling, OppretterMote " +
				"where Moteinnkalling.AvtaleID = OppretterMote.AvtaleID " +
				"and Moteleder = " + "\"" + username + "\"" +
				"and Status = " + "\"" + "Avslatt" + "\"";
		ResultSet rs = db.makeSingleQuery(query);
		setResultSet(rs);
		int aID = -1;
		String deltaker = "";
		
		while (next()) {
			aID = getInt("AvtaleID");
			deltaker = getString("Deltaker");
			String query2 = "select Beskrivelse " +
					"from Avtale " +
					"where Avtale.AvtaleID = " + aID;
			ResultSet rs2 = db.makeSingleQuery(query2);
			setResultSet(rs2);
			next();
			String[] choices = {"Finne nytt tidspunkt", "Avlyse motet", "Fjerne " + deltaker + " fra moteinnkallelsen"};
			String msg = "Moteinnkallelse med beskrivelse: " + "\"" + getString("Beskrivelse") + "\"" + " er blitt avslatt av " + deltaker + "." + " Hva onsker du a gjore?";
			int input = showChoices(choices, msg, "Avslatt moteinnkallelse");
			
			if (input == 0) {
				changeMoteinnkalling(aID);
			}
			else if (input == 1) {
				cancelMeeting(aID);
			}
			else {
				removeParticipant(aID, deltaker);
			}
		}
		closeDBConnection(db);
	}
	
	private void removeParticipant(int aID, String deltaker) {
		DBConnection db = new DBConnection();
		String query = "delete from Moteinnkalling " +
				"where Deltaker = " + "\"" + deltaker + "\"" +
				"and AvtaleID = " + aID;
		db.makeUpdate(query);
	}
	
	public void cancelMeeting(int aID) {
		DBConnection db = new DBConnection();
		String beskrivelse = "";
		ArrayList<String> deltakere = new ArrayList<String>();
		String query = "delete from Avtale " +
				"where AvtaleID = " + aID;
		String query2 = "Delete from OppretterMote " +
				"where AvtaleID = " + aID;
		String query3 = "select Beskrivelse, Deltaker from Avtale, Moteinnkalling " +
				"where Avtale.AvtaleID = Moteinnkalling.AvtaleID " +
				"and Avtale.AvtaleID = " + aID + " " +
						"and status = " + "\"" + "Godtatt" + "\"";
		ResultSet rs = db.makeSingleQuery(query3);
		setResultSet(rs);
		while (next()) {
			beskrivelse = getString("Beskrivelse");
			deltakere.add(getString("Deltaker"));
		}
		db.makeUpdate(query);
		db.makeUpdate(query2);
		
		for (int i = 0; i < deltakere.size(); i++) {
			String beskrivelseInsert = "" + "\"" + "Avtale med beskrivelse: " + beskrivelse + ", er avlyst" + "\"";
			
			String query4 = "insert into Melding(MottakerID,Beskrivelse) " +
				"values(" + "\"" + deltakere.get(i) + "\"" + ", " + beskrivelseInsert + ")";
			db.makeUpdate(query4);
		}
		closeDBConnection(db);
		}
		
	public void changeMoteinnkalling(int aID) {
		DBConnection db = new DBConnection();
		String[] choices = {"Dato", "Starttidspunkt", "Sluttidspunkt", "Ferdig"};
		String message = "Hva vil du endre?";
		String dato = "";
		String start = "";
		String slutt = "";
		String query = "select Dato, Starttidspunkt, Sluttidspunkt " +
				"from Avtale where AvtaleID = " + aID;
		ResultSet rs = db.makeSingleQuery(query);
		setResultSet(rs);
		if (next()) {
			dato = getString("Dato");
			start = getString("Starttidspunkt");
			slutt = getString("Sluttidspunkt");
		}
		int input;
		do {
			input = showChoices(choices, message, "Endre Moteinnkalling");
			if (input == 0) {
				String newDate = Options.inputDialog("Ny Dato", dato);
				String query2 = "update Avtale " +
						"set Dato = " + "\"" + newDate + "\"" +
						" where AvtaleID = " + aID;
				db.makeUpdate(query2);
				String query3 = "update Moteinnkalling " +
						"set Status = " + "\"" + "Avventer" + "\"" +
						" where AvtaleID = " + aID;
				db.makeUpdate(query3);
			} 
			else if (input == 1) {
				String newStart = Options.inputDialog("Nytt starttidspunkt", start);
				String query2 = "update Avtale " +
								"set Starttidspunkt = " + "\"" + newStart + "\"" +
								" where AvtaleID = " + aID;
				db.makeUpdate(query2);
				String query3 = "update Moteinnkalling " +
								"set Status = " + "\"" + "Avventer" + "\"" +
								" where AvtaleID = " + aID;
				db.makeUpdate(query3);
			} 
			else if (input == 2) {
				String newSlutt = Options.inputDialog("Nytt sluttidspunkt", slutt);
				String query2 = "update Avtale " +
								"set Sluttidspunkt = " + "\"" + newSlutt + "\"" +
								" where AvtaleID = " + aID;
				db.makeUpdate(query2);
				String query3 = "update Moteinnkalling " +
								"set Status = " + "\"" + "Avventer" + "\"" +
								" where AvtaleID = " + aID;
				db.makeUpdate(query3);
			}
		} while (input != 3);
		closeDBConnection(db);
	}
	
	public boolean hasMessage(String username) {
		DBConnection db = new DBConnection();
		String query = "select ID from Melding " +
				"where MottakerID = " + "\"" + username + "\"";
		ResultSet rs = db.makeSingleQuery(query);
		setResultSet(rs);
		if (next()) {
			closeDBConnection(db);
			return true;
		}
		closeDBConnection(db);
		return false;
	}
	
	public void showMessage(String username) {
		DBConnection db = new DBConnection();
		String beskrivelse = "";
		String query = "select Beskrivelse from Melding " +
				"where MottakerID = " + "\"" + username + "\"";
		ResultSet rs = db.makeSingleQuery(query);
		setResultSet(rs);
		beforeFirst();
		while (next()) {
			beskrivelse = getString("Beskrivelse");
			messageDialog(caller, beskrivelse);
			System.out.println("Kjorer");
		}
			
		String query2 = "delete from Melding " +
				"where MottakerID = " + "\"" + username + "\"";
		db.makeUpdate(query2);
		closeDBConnection(db);
	}
}